package com.dp.appi.controller;

import com.dp.appi.entity.UserEntity;
import com.dp.appi.exception.CommonException;
import com.dp.appi.service.ConsumerService;
import com.dp.appi.service.FootprintService;
import com.dp.appi.util.ExceptionUtil;
import com.dp.appi.util.SYS;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Map;

/** 统计项目数
 * Created by bchen on 2016/5/26.
 */

@Controller
@RequestMapping(value = "/footprintControl")
public class FootprintControl {
    private static Logger log = LoggerFactory.getLogger(FootprintControl.class);
    @Autowired
    private FootprintService footprintService;

    @Autowired
    private ConsumerService consumerService;

    /**
     * 按年统计项目数
     * @param modelMap
     * @param param
     */
    @RequestMapping(value = "/stageNumByYear",method = RequestMethod.GET)
    public void stageNumByYear(ModelMap modelMap, @RequestParam Map<String,String> param) {
        try {
            String openIdent = param.get("openIdent");
            UserEntity userEntity = UserEntity.decrypt(openIdent);
            Map<String, Object> map = consumerService.getUser(userEntity);
            String user_id = String.valueOf(map.get("id"));
            modelMap.addAttribute("stageNumByYear",footprintService.stageNumByYear(user_id));
        }catch (CommonException c) {
            modelMap.addAllAttributes(SYS.valueOf(c.getMessage()).msgMap());
        }catch (Exception e){
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            log.error(ExceptionUtil.getExceptionAllinformation(e));
        }
    }

    /**
     * 按年统计项目数和文章发布数
     * @param modelMap
     * @param param
     */
    @RequestMapping(value = "/stageArticleFoot",method = RequestMethod.GET)
    public void stageArticleFoot(ModelMap modelMap, @RequestParam Map<String,String> param) {
        try {
            String openIdent = param.get("openIdent");
            String year = param.get("year");
            UserEntity userEntity = UserEntity.decrypt(openIdent);
            Map<String, Object> map = consumerService.getUser(userEntity);
            String user_id = String.valueOf(map.get("id"));
            modelMap.addAllAttributes(footprintService.stageArticleFoot(user_id,year));
        }catch (CommonException c) {
            modelMap.addAllAttributes(SYS.valueOf(c.getMessage()).msgMap());
        }catch (Exception e){
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            log.error(ExceptionUtil.getExceptionAllinformation(e));
        }
    }


    /**
     * 工作统计
     * @param modelMap
     * @param param
     */
    @RequestMapping(value = "/workStatistics",method = RequestMethod.GET)
    public void workStatistics(ModelMap modelMap, @RequestParam Map<String,String> param) {
        try {
            String openIdent = param.get("openIdent");
            UserEntity userEntity = UserEntity.decrypt(openIdent);
            Map<String, Object> map = consumerService.getUser(userEntity);
            String user_id = String.valueOf(map.get("id"));
            modelMap.addAllAttributes(footprintService.workStatistics(user_id));
        }catch (CommonException c) {
            modelMap.addAllAttributes(SYS.valueOf(c.getMessage()).msgMap());
        }catch (Exception e){
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            log.error(ExceptionUtil.getExceptionAllinformation(e));
        }
    }

}
